//
//  KunBaseVC.swift
//  YaoXinCalculator
//
//  Created by Li Dong on 2023/6/1.
//

import UIKit
import FDFullscreenPopGesture
import SnapKit

class KunBaseVC: UIViewController {
    
    var toolbarHairlineImageView: UIImageView?
    var hasLoadAd = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .init(hex: 0x3388FA)
        if let temp = navigationController?.navigationBar {
            toolbarHairlineImageView = hairlineImageViewInToolbar(view: temp)
        }
    }
    
    
//    lazy var adHelper: ADManager = {
//        return ADManager()
//    }()

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        toolbarHairlineImageView?.isHidden = true
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        .default
    }
    
    private func hairlineImageViewInToolbar(view: UIView) -> UIImageView? {
        if view.isKind(of: UIImageView.self) && view.bounds.height <= 1.0 {
            return (view as! UIImageView)
        }
        let subviews = (view.subviews as [UIView])
        for subview: UIView in subviews {
            if let imageView: UIImageView = hairlineImageViewInToolbar(view: subview) {
                    return imageView
                }
            }
        return nil
    }
    
    func hideNavgaitionBar()  {
        navigationController?.setNavigationBarHidden(true, animated: false)
        fd_prefersNavigationBarHidden = true
    }
    
    var showPopBtn: Bool = false {
        didSet {
            view.addSubview(popBtn)
            popBtn.snp.makeConstraints { make in
                make.left.equalTo(5)
                make.width.height.equalTo(44)
                make.top.equalTo(CGFloat.topPadding)
            }
        }
    }
    
    var navTitle: String = "" {
        didSet {
            titleLabel.text = navTitle
            view.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(CGFloat.topPadding)
                make.height.equalTo(44)
            }
        }
    }
    
    var showBgImgView: Bool = false {
        didSet {
            view.insertSubview(bgImgView, at: 0)
            bgImgView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
        }
    }
    


    lazy var bgImgView: UIImageView = {
        let temp = UIImageView()
        temp.contentMode = .scaleAspectFill
        temp.clipsToBounds = true
        temp.image = UIImage(named: "bj")
        return temp
    }()
    
    lazy var popBtn: UIButton = {
        let temp = UIButton()
        temp.setImage(UIImage(named: "arrow_back"), for: .normal)
        temp.addTarget(self, action: #selector(popHandler), for: .touchUpInside)
        return temp
    }()
    

    
    lazy var titleLabel: UILabel = {
        let temp = UILabel()
        temp.textColor = .white
        temp.font = UIFont.Medium(ofSize: 18)
        return temp
    }()
    
    @objc func popHandler() {
        if let nav = navigationController {
            nav.popViewController(animated: true)
        }else{
            dismiss(animated: true, completion: nil)
        }
    }
    
    @objc func rightBtnHandler() {
        
    }

}
